package clone;

import java.io.*;

/**
 * https://www.cnblogs.com/Qian123/p/5710533.html
 * 序列化  克隆
 *
 */
public class Outer implements Serializable {
    private static final long serialVersionUID = 446933054411675857L;

public  Inner inner;
public  Outer myClone() {
    Outer outer = null;
    try {
        //将对象序列化成流 因为写在流里的对象是对一个对象的拷贝 而原对象依然存在于JVM
//        里面
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(this);
        // j将流序列化成对象
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        outer = (Outer) ois.readObject();

    } catch (IOException i) {
        i.printStackTrace();
    } catch (ClassNotFoundException c) {
        c.printStackTrace();
    }
return  outer;

}

    public Outer(Inner inner) {
        this.inner = inner;
    }
}



